package com.xiebishe.mylibrary.common.glide;

import android.text.TextUtils;

import java.net.MalformedURLException;

public class UrlRequestOption {
    private String url;
    private int sourceWidth;
    private int sourceHeight;
    private boolean needCutting;
    private boolean support7NExtend;
    private String blur;
    private String thumbnail;

    public UrlRequestOption() {
    }

    public static UrlRequestOption of(String url) {
        UrlRequestOption urlRequestOption = new UrlRequestOption();
        urlRequestOption.url = url;
        return urlRequestOption;
    }

    public UrlRequestOption setUrl(String url) {
        this.url = url;
        return this;
    }

    public UrlRequestOption setSourceWidth(int sourceWidth) {
        this.sourceWidth = sourceWidth;
        return this;
    }

    public UrlRequestOption setSourceHeight(int sourceHeight) {
        this.sourceHeight = sourceHeight;
        return this;
    }

    public UrlRequestOption setNeedCutting(boolean needCutting) {
        this.needCutting = needCutting;
        return this;
    }

    public UrlRequestOption setSupport7NExtend(boolean support7NExtend) {
        this.support7NExtend = support7NExtend;
        return this;
    }

    public UrlRequestOption setBlur(String blur) throws MalformedURLException {
        if (!this.support7NExtend) {
            throw new MalformedURLException("请把support7NExtend属性设成true才能使用");
        } else {
            this.blur = blur;
            return this;
        }
    }

    public UrlRequestOption setThumbnail(String thumbnail) throws MalformedURLException {
        if (!this.support7NExtend) {
            throw new MalformedURLException("请把support7NExtend属性设成true才能使用");
        } else {
            this.thumbnail = thumbnail;
            return this;
        }
    }

    public String build() {
        return this.parseImageUrl(this.url, this.sourceWidth, this.sourceHeight, this.needCutting);
    }

    private String parseImageUrl(String url, int width, int height, boolean needCutting) {
        if (TextUtils.isEmpty(url)) {
            return "";
        } else if (url.startsWith("local:")) {
            return url.substring(url.indexOf("/"));
        } else {
            StringBuilder newUrlBuilder = new StringBuilder(url);
            return newUrlBuilder.toString();
        }
    }

}

